package com.zwd.cases.controller;

import com.zwd.cases.common.constant.ResponseDto;
import com.zwd.cases.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;

/**
 * @Description 文件上传
 * @Author zhengwd
 * @Date 2022/7/21 16:30
 **/
@Slf4j
@Controller
@RequestMapping("testup")
public class UploadController {

    public static final String BUSINESS_NAME = "文件上传";

    /**
     * 单个文件上传页面
     * @return
     */
    @RequestMapping("/uploadForm")
    public String uploadForm() {
        return "/test/upload";
    }

    /**
     * 单个文件上传页面
     * @return
     */
    @RequestMapping("/list")
    public ResponseDto list(User user) {
        ResponseDto responseDto = new ResponseDto();
        responseDto.setContent(user);
        return responseDto;
    }

    /**
     * 单个文件上传
     * @param file
     * @return
     */
    @RequestMapping(value="/upload",method=RequestMethod.POST,consumes="multipart/form-data")
    public ModelAndView uploadFile(@RequestParam MultipartFile file) {
        ModelAndView mv = new ModelAndView("/test/uploadResult");
        String message;
        if (file.isEmpty()) {
            message = "Plse select file";
            mv.addObject("message", message);
            return mv;
        }
        try {
            String fileName = file.getOriginalFilename();
            // 上传文件存储位置
            String destFileName = "D:\\whupload" + File.separator + fileName;
            File destFile = new File(destFileName);
            file.transferTo(destFile);
            // 文件上传成功显示
            message = "upload file success.";
        } catch (Exception e) {
            // 文件上传失败显示
            message = "upload file fail";
            log.debug(e.getMessage());
        }
        mv.addObject("message", message);
        return mv;
    }

    /**
     * 单个文件上传页面
     * @return
     */
    @RequestMapping("/uploadBatchFileForm")
    public String uploadBatchFileForm() {
        return "/test/uploadBatch";
    }

    /**
     * 多个文件上传
     * @param files
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value="/uploadBatchFile", method=RequestMethod.POST, consumes="multipart/form-data")
    public String uploadBatchFile(@RequestParam MultipartFile[] files, RedirectAttributes redirectAttributes) {
        boolean isEmpty = true;
        try {
            for (MultipartFile multipartFile : files) {
                if (multipartFile.isEmpty()) {
                    continue;
                }
                String fileName = multipartFile.getOriginalFilename();
                String destFileName = "D:\\whupload" + File.separator + fileName;
                File destFile = new File(destFileName);
                multipartFile.transferTo(destFile);
                isEmpty = false;
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "upload file fail");
            log.debug(e.getMessage());
            return "redirect:/testup/uploadBatchFileForm";
        }
        if (isEmpty) {
            redirectAttributes.addFlashAttribute("message", "Plse select file");
        } else {
            redirectAttributes.addFlashAttribute("message", "upload file success.");
        }
        return "redirect:/testup/uploadBatchFileForm";
    }

}
